﻿@inject NotificationService NotificationService
@inject ModalService ModalService

<div class="d-flex flex-row justify-content-between">
    <h1>@Title</h1>
    <div class="btn-group-toggle btn-group btn-group-lg my-auto" role="group">
        @if (IsCreate)
        {
            <button type="button" class="btn btn-success mdi mdi-plus" @onclick="@onCreate">Create</button>
        }
        else
        {
            <button type="button" class="btn btn-primary mdi mdi-content-save" @onclick="@onUpdate">Save</button>
            <button type="button" class="btn btn-danger mdi mdi-trash-can" @onclick="@onDelete">Delete</button>
        }
    </div>
</div>

@ChildContent

@code {
    [Parameter]
    public string Title { get; set; }

    [Parameter]
    public bool IsCreate { get; set; }

    [Parameter]
    public RenderFragment ChildContent { get; set; }

    [Parameter]
    public EventCallback<EventCallbackResult<bool>> OnCreate { get; set; }

    [Parameter]
    public EventCallback<EventCallbackResult<bool>> OnUpdate { get; set; }

    [Parameter]
    public EventCallback<EventCallbackResult<bool>> OnDelete { get; set; }

    [Parameter]
    public EventCallback<ModelValidation> OnCheckValidation { get; set; }

    private async Task<bool> onCheckValidation()
    {
        ModelValidation mv = new ModelValidation();
        await OnCheckValidation.InvokeAsync(mv);
        if (mv.Errors.Count > 0)
        {
            await NotificationService.Error(new NotificationConfig
            {
                Message = "Invalid data",
                Description = string.Join('\n', mv.Errors)
            });
        }
        return mv.Errors.Count is 0;
    }

    private async Task onCreate()
    {
        if (!await onCheckValidation()) return;

        if (OnCreate.HasDelegate)
        {
            EventCallbackResult<bool> result = new EventCallbackResult<bool>();
            await OnCreate.InvokeAsync(result);
            if (result.Result)
            {
                await NotificationService.Success(new NotificationConfig
                {
                    Message = "Created",
                });
            }
            else
            {
                await NotificationService.Error(new NotificationConfig
                {
                    Message = "Create failed",
                });
            }
        }
    }

    private async Task onUpdate()
    {
        if (!await onCheckValidation()) return;

        if (OnUpdate.HasDelegate)
        {
            EventCallbackResult<bool> result = new EventCallbackResult<bool>();
            await OnUpdate.InvokeAsync(result);
            if (result.Result)
            {
                await NotificationService.Success(new NotificationConfig
                {
                    Message = "Updated",
                });
            }
            else
            {
                await NotificationService.Error(new NotificationConfig
                {
                    Message = "Update failed",
                });
            }
        }
    }

    private void onDelete()
    {
        async Task OnOk(AntDesign.ModalClosingEventArgs e)
        {
            if (OnDelete.HasDelegate)
            {
                EventCallbackResult<bool> r = new EventCallbackResult<bool>();
                await OnDelete.InvokeAsync(r);
                if (r.Result)
                {
                    await NotificationService.Success(new NotificationConfig
                    {
                        Message = "Deleted",
                    });
                }
                else
                {
                    await NotificationService.Error(new NotificationConfig
                    {
                        Message = "Delete failed",
                    });
                }
            }
        }

        ModalService.Confirm(new ConfirmOptions()
        {
            Title = "Are you sure delete?",
            Content = "Delete confirm",
            OnOk = OnOk,
            OkText = "Yes",
            OkType = "danger",
            CancelText = "No",
        });
    }
}
